Mailbox representation
Mailbox Representation
Definition
Mailbox representation is a classic way of modeling the chessboard inside a chess program using a one-dimensional array that contains the board squares plus extra “guard” squares. These sentinel squares make it easy to detect when a move goes off the board. The most common mailbox schemes are the 10×12 array (120 squares) and the 0x88 array (128 squares).
How It Works
Instead of storing just the 64 legal squares, a mailbox board stores an “expanded” board with a border. When a move generation routine steps a piece along a direction, it uses simple index increments. If a step lands on a sentinel (off-board) square, the move generator knows to stop.
-
10×12 (120-square) mailbox:
- Array indexes are arranged as 10 files by 12 ranks. The outer ring is off-board.
- Directional steps are intuitive: +10 (north), −10 (south), +1 (east), −1 (west).
- Off-board detection: any read of a guard square is recognized by a special value.
-
0x88 (128-square) mailbox:
- Array indexes form an 8×16 grid (128 slots). The valid 8×8 board occupies indexes where (index & 0x88) == 0.
- Directional steps are powers of two and small integers: +16 (north), −16 (south), +1 (east), −1 (west), +15/+17/−15/−17 (diagonals).
- Off-board detection: test (to & 0x88) != 0; if true, the square is off-board.
Usage in Chess Programming
Mailbox representation is used to generate pseudo-legal moves, validate legality (e.g., king safety), and implement make/unmake routines. It provides a compact, readable way to scan directions for sliding pieces (bishops, rooks, queens) and to apply fixed-offset moves for knights and kings. Many tutorial engines adopt mailbox because it simplifies bounds checking and the core ideas of move generation.
- Sliding pieces: repeatedly add a direction increment until a blocker or off-board sentinel is reached.
- Knights/kings: test a small set of precomputed index offsets; discard off-board hits via sentinel checks.
- Special moves: castling, promotion, and en passant can be handled by combining mailbox scans with simple state variables (castling rights, en passant square).
Examples
Typical move increments and offsets:
- 10×12 mailbox:
- Rook directions: +10, −10, +1, −1
- Bishop directions: +9, +11, −9, −11
- Queen directions: combine rook and bishop increments
- Knight offsets: ±21, ±19, ±12, ±8
- 0x88 mailbox:
- Rook directions: +16, −16, +1, −1
- Bishop directions: +15, +17, −15, −17
- Queen directions: combine rook and bishop increments
- Knight offsets: ±33, ±31, ±18, ±14
Visualizing a knight’s fixed offsets from c3 (all legal target squares are two-and-one away):
For a rook on a4 in a 10×12 board, a move generator would scan +10 (north) until a blocker or sentinel, then −10 (south), +1 (east), and −1 (west) the same way, adding captures and quiet moves as encountered.
Strategic and Historical Significance
While “strategy” lives at the chessboard, representation shapes what an engine can do efficiently. In the 1980s–2000s, mailbox dominated hobby and early commercial engines because it’s easy to implement and debug on limited hardware. Tutorial engines such as TSCP (10×12) and micro-Max (0x88) popularized it for learners. As 64-bit CPUs and fast bitwise instructions (e.g., POPCNT) became ubiquitous, bitboard representation rose to prominence in top engines like Stockfish, thanks to faster attack generation and bulk operations. Still, mailbox remains a practical choice for compact or educational engines and embedded targets.
Pros and Cons
- Pros:
- Simple, readable move generation; minimal boundary checks.
- Easy to prototype, debug, and reason about per piece.
- Compact state; good fit for small or teaching engines.
- Cons:
- Generally slower than high-quality bitboards for large-scale attack queries and mobility counting.
- Harder to exploit modern CPU features (SIMD, wide bitwise ops, hardware popcount).
- Sliding attacks often require loops with branches, which can be less cache- and branch-predictor-friendly.
Interesting Facts
- The name “mailbox” evokes an array of slots (like mailboxes) holding piece codes, plus “no-entry” boxes around the edges.
- The 0x88 trick is named after the hexadecimal mask 0x88; any index with the 0x88 bit set is off the 8×8 board.
- Engine tutorials often start with mailbox because perft testing (counting legal move trees) is straightforward to implement and verify.
- Many minimal engines that fit on a single page of code use 0x88 for its elegant off-board check and concise move offsets.